<template >
  <el-container class="home_container">
    <el-header>
      <div class="logo_left">
        <img src="../assets/logo2.png" alt />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="zs?'64px' : '200px'">
        <div class="toggle-button"  @click="zhanshi">|||</div>
        <!-- 侧边栏 -->
         <!-- collapse-transition是表示取消组件的动画
              router是给每个导航添加路由，其中菜单标签中index值表示的就是路由
               default-active高亮显示所选项
         -->
        <el-menu
          background-color="#363d40"
          text-color="#fff"
          active-text-color="#3F86D8"
          unique-opened
          :collapse="zs"
          :collapse-transition="false"
          :router="true" 
          :default-active="activePaths"
        >
          <el-submenu :index="item.id+''" v-for="item in shangpings" :key="item.id" >
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{item.name}}</span>
            </template>
            <!-- 二级菜单  这里后面加了引号是为了让他转换成字符串-->
          <el-menu-item :index="'/'+item.path+''" v-for="item in item.children" :key="item.getname"
           @click="getPath('/'+item.path+'')"
          >
               <template slot="title">
                   <!-- 图标 -->
                <i class="el-icon-menu"></i>
                   <!-- 文本 -->
                <span>{{ item.getname }}</span>
               </template>
          </el-menu-item>
           
          </el-submenu>
        
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 导入welcom组件 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return {
      shangpings:[],
      icons:{
        "1":"el-icon-user-solid",
        "2":"el-icon-s-platform",
        "3":"el-icon-s-goods",
        "4":"el-icon-s-order",
        "5":"el-icon-s-marketing"
      },
      zs:false,
      // 二级菜单高亮效果
      activePaths:''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    zhanshi(){
      this.zs = !this.zs
    },
    // 获取所有的菜单 这里的async要和await配合使用，其实就是简化promesi
    async  getshangp(){
      const {data:res} = await this.$http.get('shangping')
      console.log(res)
      this.shangpings = res
    },
    getPath(activePath){
      window.sessionStorage.setItem("path",activePath)
      this.activePaths = activePath
    }
  },
  created(){
    this.getshangp()
    this.activePaths = window.sessionStorage.getItem("path")
  }
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #363d40;
  .el-menu{
    border-right:none;
  }
}
.el-main {
  background-color: #e9edf1;
}
.logo_left {
  display: flex;
  align-items: center;
  img {
    width: 50px;
    height: 50px;
    margin-right: 15px;
  }
  span {
    color: white;
  }
}
.toggle-button{
  background-color:#A6A9AD;
  text-align: center;
  letter-spacing:3px;
  padding:3px;
}
</style>